# In Government We Trust: Implicit Political Trust and Regime Support in China 
# Figures 2-4 replication file

# install package if not installed already
install.packages("ggplot2")

# load libraries
library(ggplot2)

# load data, fyi: change file location
data <-read.csv("--file path--/IAT_China_replication_data.csv")

# mean trust index
mean_trustindex <- mean(data$trust_index)
mean_trustindex

# plot density trust index
den <- density(data$trust_index)
plot(den, main = NA, xlab = NA)
polygon(den, col = "gray", border = "black") 
abline(v = mean_trustindex, lty = 2, lwd = 1.5)
text(53, 0.02, "Mean ->", cex = .9)
title(main = "Density of Trust Index", adj = 0)

# mean single target trust iat
mean_diatgov <- mean(data$diatgov)
mean_diatgov

# plot density single target trust iat
den1 <- density(data$diatgov)
plot(den1, main = NA, xlab = NA) 
polygon(den1, col = "gray", border = "black") 
abline(v = mean_diatgov, lty = 2, lwd = 1.5)
text(0.5, 0.6, "<- Mean", cex = .9)
title(main = "Density of Trust ST-IAT D-Scores", adj = 0)

# plot of trust index and single target trust iat
compareplot <- ggplot(data, aes(trust_index, y=diatgov)) + 
  geom_jitter(position = position_jitter(width = 1)) + 
  geom_smooth(method=loess, span=.5, level=.95) + 
  theme_bw() + 
  labs(x = "Explicit Trust", y = "Implicit Trust D-Score") + 
  theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())  + 
  geom_rug(col=rgb(.7,0,.7,alpha=.15), sides="l") + 
  ylim(-1.7, 1.7) + 
  theme(plot.margin = unit(c(2,2,2,2), "mm"))
compareplot
